﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Text;

namespace SourceGenerator
{
    /// <summary>
    /// 引用时需要在工程文件中修改
    ///<ItemGroup>
    ///     <ProjectReference Include = "..\CustomGenerator\CustomGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
    ///</ItemGroup>
    ///
    /// </summary>
    [Generator]
    public class CustomGenerator : ISourceGenerator
    {
        public void Initialize(GeneratorInitializationContext context) { }

        public void Execute(GeneratorExecutionContext context)
        {
            context.AddSource("myGeneratedFile.cs", SourceText.From(@"
namespace GeneratedNamespace
{
    public class GeneratedClass
    {
        public static void GeneratedMethod()
        {
            // generated code
            Console.WriteLine(""11111"");
        }
    }
}", Encoding.UTF8));
        }
    }
}
